package demo1.servlet.userMai;

import com.alibaba.fastjson.JSON;
import demo1.pojo.fileList;
import demo1.pojo.goods;
import demo1.pojo.users;
import demo1.service.goodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

@WebServlet("/uploadPicture")
public class uploadPicture extends HttpServlet {//上传照片
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //接受数据
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串

        //转为goods对象
        fileList fileList= (JSON.parseObject(params, fileList.class));

        goodsService goodsService = new goodsService();
        int num = 0;
        try {
            num = goodsService.updateGoodsUrl(fileList.getGoodsID(), fileList.getUrl());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //调用service验证
        if (num == 1) {
            //写入成功
            resp.getWriter().write("success");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
